﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using Lvzhi.Core;
using Lvzhi.Entities;
using Lvzhi.Entities.BaseEnum;
using Lvzhi.Provide.EncryptHelper;
using Lvzhi.Provide.StringHelper;
using Webdiyer.WebControls.Mvc;
using Lvzhi.AdminCenter.Extension.MvcHelper;

namespace Lvzhi.AdminCenter.Controllers
{
    public class UserController : BaseController
    {
        private UserCore ucore = new UserCore();
        private string defaultpwd = ConfigurationManager.AppSettings["DefaultPwd"];
        /// <summary>
        /// 个人中心
        /// </summary>
        /// <returns></returns>
        public ActionResult Info(int id = 1, int size = 10, string key = "")
        {
            return View();
        }
        /// <summary>
        /// 中间商
        /// </summary>
        /// <returns></returns>
        public ActionResult Agent(int id = 1, int size = 10, string key = "")
        {

            Expression<Func<Core_User, Boolean>> expr = t => t.U_Role == (int)ManagerRoleEnum.中间代理商;
            var user = GetUser();
            if (GetUser().U_Role != 0)
            {
                expr = expr.And(t => t.U_PId == user.U_Id);
            }
            if (!string.IsNullOrWhiteSpace(key))
            {
                expr = expr.And(t => t.U_Name.Contains(key.Trim()));
            }
            var list = ucore.LoadEntities(expr).OrderByDescending(o => o.U_Id).ToPagedList(id, size);
            return View(list);
        }

        /// <summary>
        /// 出单员
        /// </summary>
        /// <returns></returns>
        public ActionResult Worker(int id = 1, int size = 10, string key = "")
        {

            Expression<Func<Core_User, Boolean>> expr = t => t.U_Role == (int)ManagerRoleEnum.终端销售商;
            var user = GetUser();
            if (GetUser().U_Role != 0)
            {
                expr = expr.And(t => t.U_PId == user.U_Id);
            }

            if (!string.IsNullOrWhiteSpace(key))
            {
                expr = expr.And(t => t.U_Name.Contains(key.Trim()));
            }
            var list = ucore.LoadEntities(expr).OrderByDescending(o => o.U_Id).ToPagedList(id, size);
            return View(list);
        }

        /// <summary>
        /// 用户管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(int id = 1, int size = 10, string key = "")
        {
            Expression<Func<Core_User, Boolean>> expr = t => true;
             
            if (!string.IsNullOrWhiteSpace(key))
            {
                expr = expr.And(t => t.U_Name.Contains(key.Trim()));
            }
            var list = ucore.LoadEntities(expr).OrderByDescending(o => o.U_Id).ToPagedList(id, size);
            return View(list);
        }

        /// <summary>
        /// 编辑出单用户
        /// </summary>
        /// <returns></returns>
        public ActionResult EditWorker(int? id)
        {
            ViewBag.Defaultpwd = defaultpwd;
            Core_User user = null;
            if (id.HasValue)
            {
                user = ucore.LoadEntity(o => o.U_Id == id);
            }
            else
            {
                user = new Core_User();
                user.U_Pwd = defaultpwd;
            }
            return View(user);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditWorker(Core_User obj)
        {
            obj.U_Lasttime = DateTime.Now;
            if (obj.U_Id == 0)//新增
            {
                obj.U_PId = GetUser().U_PId;
                obj.U_PName = GetUser().U_PName;
                string pwdCode = new CreateRandomStr().GetRandomString(4);//登录密码干扰码 
                obj.U_Pwd = Md5Extensions.MD5Encrypt(defaultpwd + pwdCode);
                obj.U_EncryptCode = pwdCode;
                obj.U_CreateTime = DateTime.Now;
                obj.U_Role = (int)ManagerRoleEnum.终端销售商;


                ucore.AddEntity(obj);
            }
            else
            {
                var old = ucore.LoadEntity(o => o.U_Id == obj.U_Id);

                old.U_Phone = obj.U_Phone;
                old.U_TureName = obj.U_TureName;
                old.U_Sex = obj.U_Sex;
                old.U_Tel = obj.U_Tel;
                old.U_Email = obj.U_Email;
                old.U_Fax = obj.U_Fax;
                old.U_Role = obj.U_Role;

                ucore.UpdateEntity(old);
            }

            return RedirectToAction("Worker");
        }

        /// <summary>
        /// 编辑代理商
        /// </summary>
        /// <returns></returns>
        public ActionResult EditAgent(int? id)
        {
            ViewBag.Defaultpwd = defaultpwd;
            Core_User user = null;
            if (id.HasValue)
            {
                user = ucore.LoadEntity(o => o.U_Id == id);
            }
            else
            {
                user = new Core_User();
                user.U_Pwd = defaultpwd;
            }
            return View(user);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditAgent(Core_User obj)
        {
            obj.U_Lasttime = DateTime.Now;
            if (obj.U_Id == 0)//新增
            {

                obj.U_PId = GetUser().U_Id;
                obj.U_PName = GetUser().U_Name;
                string pwdCode = new CreateRandomStr().GetRandomString(4);//登录密码干扰码 
                obj.U_Pwd = Md5Extensions.MD5Encrypt(defaultpwd + pwdCode);
                obj.U_EncryptCode = pwdCode;
                obj.U_CreateTime = DateTime.Now;
                obj.U_Role = (int)ManagerRoleEnum.中间代理商;

                ucore.AddEntity(obj);
            }
            else
            {
                var old = ucore.LoadEntity(o => o.U_Id == obj.U_Id);
                old.U_Phone = obj.U_Phone;
                old.U_TureName = obj.U_TureName;
                old.U_Sex = obj.U_Sex;
                old.U_Tel = obj.U_Tel;
                old.U_Email = obj.U_Email;

                old.U_LinkName = obj.U_LinkName;
                old.U_Fax = obj.U_Fax;
                old.U_Role = obj.U_Role;
                old.U_Address = obj.U_Address;
                old.U_MoneyType = obj.U_MoneyType;
                old.U_PostCode = obj.U_PostCode;

                ucore.UpdateEntity(old);
            }

            return RedirectToAction("agent");
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult EditPwd(int id)
        {

            Core_User user = ucore.LoadEntity(o => o.U_Id == id);
            return View(user);
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditPwd(Core_User obj)
        {
            string tip = "";
            Core_User user = ucore.LoadEntity(o => o.U_Id == obj.U_Id);

            string oldpwd = Request.Form["oldPwd"];
            string newpwd = Request.Form["txtNewpwd"].ToString();
            string newpwd2 = Request.Form["txtNewpwd2"].ToString();
            if (newpwd != newpwd2 && !string.IsNullOrEmpty(newpwd))
            {
                tip = "设置密码不正确";
            }
            else if (!user.U_Pwd.Equals(Md5Extensions.MD5Encrypt(oldpwd + user.U_EncryptCode)))
            {

                tip = "原密码不正确";
            }
            else
            {
                user.U_Pwd= Md5Extensions.MD5Encrypt(newpwd + user.U_EncryptCode);//修改密码

                ucore.UpdateEntity(user);
                tip = "修改密码成功";
            }
            ViewBag.Tip = tip;
            return Redirect("/user/edit/"+obj.U_Id);
            return View(obj);
        }
        /// <summary>
        /// 冻结
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpPost]
        public string Forzen(int id)
        {
            var info = ucore.LoadEntity(o => o.U_Id == id);

            if (info != null)
            {
                var state = info.U_State;
                if (state == (int)UserStatusEnum.冻结)
                {
                    state = (int)UserStatusEnum.正常;
                }
                else if (state == (int)UserStatusEnum.正常)
                {
                    state = (int)UserStatusEnum.冻结;
                }
                info.U_State = state;
                if (ucore.UpdateEntity(info)) //更改状态
                {
                    return "ok";
                }
            }
            return "更新数据失败";
        }

    }
}